package com.terrace.db.system.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.terrace.core.spring.datasource.annotation.DataSource;
import com.terrace.core.spring.ehcache.annotation.EhCacheEvict;
import com.terrace.core.spring.ehcache.annotation.EhCacheable;
import com.terrace.db.constant.DBType;
import com.terrace.db.system.dao.PermissionDao;
import com.terrace.db.system.dao.RolePermissionDao;
import com.terrace.db.system.mapper.entity.Permission;
import com.terrace.db.system.service.PermissionService;
import com.terrace.core.mybatis.entity.Pagination;
import com.terrace.core.mybatis.plugin.pagination.PaginationHelper;

@Service
@Transactional
@DataSource(name = DBType.ADMIN)
public class PermissionServiceImpl implements PermissionService {

	@Autowired
	private PermissionDao permissionDao;
	
	@Autowired
	private RolePermissionDao rolePermissionDao;
	
	@Override
	public int savePermissionSelective(Permission permission) {
		return permissionDao.savePermissionSelective(permission);
	}
	
	@Override
	@EhCacheEvict(key = "userVisitMenuList")
	public int batchDeleteRolePermissionByRoleId(String roleId, List<String> permiIds) {
		return rolePermissionDao.batchDeleteRolePermissionByRoleId(roleId, permiIds);
	}

	@Override
	@EhCacheEvict(key = "userVisitMenuList")
	public int batchSaveRolePermission(String roleId, List<String> permiIds) {
		return rolePermissionDao.batchSaveRolePermission(roleId, permiIds);
	}
	
	@Override
	public int deleteByPermissionId(String id) {
		return permissionDao.deleteByPermissionId(id);
	}

	@Override
	public int updateByPermissionIdSelective(Permission permission) {
		return permissionDao.updateByPermissionIdSelective(permission);
	}

	@Override
	public List<Permission> selectPermissionsByUserId(String userId) {
		return permissionDao.selectPermissionsByUserId(userId);
	}
	
	@Override
	public List<String> selectPermiIdsByCondition(Map<String, Object> condition) {
		return permissionDao.selectPermiIdsByCondition(condition);
	}
	
	@Override
	public Permission selectByPermissionId(String id) {
		return permissionDao.selectByPermissionId(id);
	}

	@Override
	@EhCacheable(key = "userVisitMenuList")
	public List<Permission> selectVisitMenuByUserId(String userId) {
		return permissionDao.selectVisitMenuByUserId(userId);
	}
	
	@Override
	public List<Permission> selectPermissionsBySelective(Permission permission) {
		return permissionDao.selectPermissionsBySelective(permission);
	}

	@Override
	public Pagination<Permission> selectPermissionsForPagination(Permission permission, Pagination<Permission> pagination) {
		PaginationHelper.prepare(pagination);
		return PaginationHelper.execute(permissionDao.selectPermissionsForPagination(permission));
	}

}
